package dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.User;
import util.HibernateUtil;

public class UserDao {

	public static void save1() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(9);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tran = session.beginTransaction();

			session.save(user);

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			if(session!=null){
				session.close();
			}
		}

	}

	
	public static void save2() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(9);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSession();
			System.out.println("userdao session=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
//			HibernateUtil.closeSession();
		}

	}

	

	public static void save3() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(9);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.out.println("userdao session=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
		}

	}


	public static void save4(Session session) {

		
		Transaction tran = null;

		User user = new User();
		user.setUserid(9);
		user.setUsername("user7");
		user.setPassword("123");

		try {
		
			System.out.println("userdao session=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
		}
	}

	
	
}
